package com.atguigu.common.validator;

import com.atguigu.common.validate.ListValues;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;

public class ListValueValidator implements ConstraintValidator<ListValues,Integer> {
    HashSet<Integer> set =new HashSet<>();
    @Override
    public void initialize(ListValues constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(val);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
